<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\energy;

use app\common\dao\BaseDao;
use app\common\model\energy\EnergyMeterDayModel;

class EnergyMeterDayDao extends BaseDao
{

    /**
     * @return EnergyMeterDayModel
     */
    protected function getModel(): string
    {
        return EnergyMeterDayModel::class;
    }


    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['energy_id']) && $where['energy_id'] !== '', function ($query) use ($where) {
                $query->where('energy_id', intval($where['energy_id']));
            })
            ->when(isset($where['room_id']) && $where['room_id'] !== '', function ($query) use ($where) {
                $query->where('room_id', intval($where['room_id']));
            })
            ->when(isset($where['user_id']) && $where['user_id'] !== '', function ($query) use ($where) {
                $query->where('user_id', intval($where['user_id']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('remark', '%' . trim($where['keywords']) . '%');
            })
            ->when(isset($where['time']) && $where['time'] !== '', function ($query) use ($where) {
                $this->timeSearchBuild($query, $where['time'], 'day_time');
            });
    }

    public function getLastInfo(int $energyId)
    {
        return $this->search([
            'energy_id' => $energyId
        ])->order('id', 'desc')->find();
    }

    public function removeMeterId(int $energyId)
    {
        return $this->getSearch([
            'energy_id' => $energyId,
        ])->delete();
    }
}
